<?php
//header('content-type:text/html;charset=utf8');
//session_start();
require_once 'config.inc.php';
//print_r($_POST);
/*Array ( [uname] => hdm 
[upwd] => 123456 
[upwd1] => 123456 
[uface] => ./img/faces/AvengersAgeofUltron006.jpg 
[uemail] => huodeming1@163.com 
[uhelp] => 我是谁? 
[ureturn] => 我 
[ucode] => ra59 
[toreg] => 注 册 )*/
$sql = 'insert into users(uname,upwd,uemail,uface,uhelp,ureturn) values(?,?,?,?,?,?)';
//注册之前要验证注册码与是否是合法提交过来的
if(!empty($_POST['toreg']) && $_POST['toreg'] == '注 册' && $_POST['ucode'] == $_SESSION['code']){
	//如果验证码正确了,则开始注册
/*	try{
		$link = new PDO('mysql:host=localhost;port=3306;dbname=kp5b_blog','root','root');
		$link->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
	}catch(PDOException $e){
		die('数据库连接出错:'.$e->getMessage());
	}*/
	
	//把表单提交过来的数据写入数据库
/*Array ( [uname] => hdm 
[upwd] => 123456 
[upwd1] => 123456 
[uface] => ./img/faces/AvengersAgeofUltron006.jpg 
[uemail] => huodeming1@163.com 
[uhelp] => 我是谁? 
[ureturn] => 我 
[ucode] => ra59 
[toreg] => 注 册 )*/
	//在注册之前要查询用户名有没有,有则不注册
	$sql = 'select uid from users where uname=?';
	try{
		$stmt = $link->prepare($sql);
		$stmt->execute([$_POST['uname']]);
		if($stmt->rowCount()>0){
			die('用户名已存在,请重新填写表单!<a href="reg.php">返回</a>');
		}
	}catch(PDOException $e){
		die('查询用户是否存在时出错:'.$e->getMessage().'<a href="reg.php">返回</a>');
	}

	//进行注册
	$sql = 'insert into users(uname,upwd,uemail,uface,uhelp,ureturn) values(?,?,?,?,?,?)';
	try{
		$stmt = $link->prepare($sql);
		$stmt->execute([$_POST['uname'],md5($_POST['upwd']),$_POST['uemail'],$_POST['uface'],$_POST['uhelp'],$_POST['ureturn']]);
		echo '注册成功!';
	}catch(PDOException $e){
		die('写入数据库出错:'.$e->getMessage().'<a href="reg.php">返回</a>');
	}
}else{
	die('验证码错误或非法操作!');
}


?>